//
//  MBVipHeaderView.m
//  Meboo
//
//  Created by kyo on 2024/3/13.
//

#import "MBVipHeaderView.h"
#import "MBVipItemView.h"
#import "NSVipInfonModel.h"
@interface MBVipHeaderView ()
@property (nonatomic, strong) UIButton *leftBtn;

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) UILabel *vipNameLab;
@property (nonatomic, strong) UILabel *vipDetailLab;

@property (nonatomic, strong) NSMutableArray *numArr;
@property (nonatomic, strong) NSMutableArray *dateArr;
@property (nonatomic, strong) NSMutableArray *priceArr;
@property (nonatomic, strong) NSMutableArray <MBVipItemView *>*viewArr;
@end

@implementation MBVipHeaderView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setDataArr:(NSMutableArray *)dataArr {
    _dataArr = dataArr;
    if (dataArr.count < 3) {
        return;
    }
    
    for (int i = 1; i < dataArr.count; i++) {
        NSVipInfonModel *model = dataArr[i];
        MBVipItemView *view = self.viewArr[i-1];
//        view.NumLab.text = model.maxUses;
        [self.viewArr replaceObjectAtIndex:i-1 withObject:view];
    }
}

#pragma mark - Action
- (void)leftAction {
    [self.currentViewController.navigationController popViewControllerAnimated:YES];;
}

- (void)clickItemAction:(NSInteger)tag {
    for (MBVipItemView *view  in self.viewArr) {
        view.isSelected = view.tag == tag;
    }
}

#pragma mark - UI
- (void)setupUI {
    
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.mas_top).offset(StatusBarAndNavigationBarHeight-MBHeightScale(8));
    }];
    
    [self addSubview:self.leftBtn];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(20));
        make.centerY.equalTo(self.titleLab);
        make.width.height.mas_equalTo(MBHeightScale(20));
    }];
    
    [self addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MBHeightScale(20)+StatusBarAndNavigationBarHeight);
        make.bottom.mas_equalTo(MBHeightScale(-148));
        make.left.mas_equalTo(MBWidthScale(20));
        make.right.mas_equalTo(MBWidthScale(-20));
    }];
    
    [self.backImageView addSubview:self.vipNameLab];
    [self.vipNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(MBHeightScale(24));
        make.bottom.equalTo(self.backImageView.mas_centerY).offset(MBHeightScale(-10));
        make.left.mas_equalTo(MBWidthScale(20));
    }];
    
    [self.backImageView addSubview:self.vipDetailLab];
    [self.vipDetailLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.mas_equalTo(MBHeightScale(-24));
        make.top.equalTo(self.backImageView.mas_centerY).offset(MBHeightScale(2));
        make.left.mas_equalTo(MBWidthScale(20));
        make.width.mas_equalTo(MBWidthScale(130));
    }];
    
    [self addSubview:self.vipDateView];
    [self.vipDateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backImageView.mas_top);
        make.left.equalTo(self.backImageView);
        make.width.mas_equalTo(MBWidthScale(190));
        make.height.mas_equalTo(MBHeightScale(24));
    }];
    
    [self.vipDateView addSubview:self.dateLab];
    [self.dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.vipDateView);
    }];
    
    __WeakSelf
    CGFloat wid = (ScreenWidth-MBWidthScale(60))/3;
    for (int i = 0; i < 3; i++) {
        MBVipItemView *view = [MBVipItemView new];
        view.tag = i;
        view.NumLab.text = self.numArr[i];
        view.dateLab.text = self.dateArr[i];
        view.priceLab.text = self.priceArr[i];
        if (i == 1) {
            view.isSelected = YES;
        }
        view.clickBlock = ^(NSInteger tag) {
            [weakSelf clickItemAction:tag];
            if (weakSelf.selectBlock) {
                weakSelf.selectBlock(tag,self.priceArr[tag]);
            }
        };
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(MBWidthScale(20)+(MBWidthScale(10)+wid)*i);
            make.width.mas_equalTo(wid);
            make.height.mas_equalTo(MBHeightScale(133));
            make.bottom.equalTo(self);
        }];
        [self.viewArr addObject:view];
    }
}

#pragma mark - Setter && Getter
- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc] init];
        [_leftBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_leftBtn setImage:ImageWithName(@"mine_topic_back") forState:0];
        [_leftBtn addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = PDSemiBoldScale(20);
        _titleLab.textColor = HEXS(@"ffffff");
        _titleLab.text = @"Member activation";
    }
    return _titleLab;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.image = [UIImage imageNamed:@"vipprofile_vip_back_crown"];
    }
    return _backImageView;
}

- (UILabel *)vipNameLab {
    if (!_vipNameLab) {
        _vipNameLab = [[UILabel alloc] init];
        _vipNameLab.font = PDSemiBoldScale(18);
        _vipNameLab.textColor = HEXS(@"C07013");
        _vipNameLab.text = @"VIP Account";
    }
    return _vipNameLab;
}

- (UILabel *)vipDetailLab {
    if (!_vipDetailLab) {
        _vipDetailLab = [[UILabel alloc] init];
        _vipDetailLab.numberOfLines = 2;
        _vipDetailLab.font = PDRegularScale(16);
        _vipDetailLab.textColor = HEXS(@"C17013");
        _vipDetailLab.text = @"Get access to\nPremium features";
    }
    return _vipDetailLab;
}

- (UIImageView *)vipDateView {
    if (!_vipDateView) {
        _vipDateView = [UIImageView new];
        _vipDateView.image = ImageWithName(@"vipprofile_vip_date_back");
        _vipDateView.hidden = YES;
    }
    return _vipDateView;
}

- (UILabel *)dateLab {
    if (!_dateLab) {
        _dateLab = [[UILabel alloc] init];
        _dateLab.textAlignment = NSTextAlignmentCenter;
        _dateLab.font = PDRegularScale(12);
        _dateLab.textColor = HEXS(@"ffffff");
        _dateLab.text = [NSString stringWithFormat:@"Vip expiration time:%@",[MBUserAccount defaultAccount].vipDateStr];
    }
    return _dateLab;
}

- (NSMutableArray *)numArr {
    if (!_numArr) {
        _numArr = [NSMutableArray arrayWithArray:@[@"3",@"1",@"1"]];
    }
    return _numArr;
}

- (NSMutableArray *)dateArr {
    if (!_dateArr) {
        _dateArr = [NSMutableArray arrayWithArray:@[@"Months",@"Month",@"Week"]];
    }
    return _dateArr;
}

- (NSMutableArray *)priceArr {
    if (!_priceArr) {
        _priceArr = [NSMutableArray arrayWithArray:@[@"$ 59.99",@"$ 29.99",@"$ 19.99"]];
    }
    return _priceArr;
}

- (NSMutableArray<MBVipItemView *> *)viewArr {
    if (!_viewArr) {
        _viewArr = [NSMutableArray array];
    }
    return _viewArr;
}
@end
